package com.chenjinyan.back.controller;

import com.chenjinyan.back.entity.PicEntity;
import com.chenjinyan.back.service.PicService;
import com.chenjinyan.back.utils.ExceptionHandle;
import com.chenjinyan.back.utils.ImageUtil;
import com.chenjinyan.back.utils.ResultUtil;
import com.chenjinyan.back.vo.PicVo;
import com.chenjinyan.back.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/pic")
public class PicController {
    @Autowired
    private ExceptionHandle exceptionHandle;

    @Autowired
    PicService picService;

    @Value("${server.port}")
    //获取主机端口
    private String post;

    @Value("${server.host}")
    //获取主机ip
    private String host;

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @PostMapping("/insert")
    public Result addPic(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile multipartFile,
                         @Validated @RequestParam(value = "description", required = false, defaultValue = "") String description) {

        Result result = ResultUtil.success();
        logger.info("******图片上传接口开始******");
        String path = request.getSession().getServletContext().getRealPath("/") + "/images/upload/";
        logger.info("图片保存路径是: " + path);
        String savePath = "http://" + host + ":" + post + "/images/uplaod/";
        //保存图片到服务器
        String reqPath = null;
        try {
            if (null != multipartFile) {
                reqPath = ImageUtil.saveImg(multipartFile, path, "back", savePath);
                PicEntity picEntity = new PicEntity();
                picEntity.setDescription(description);
                picEntity.setBackPicUrl(reqPath);
                picService.insertPic(picEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/getPic")
    public Result getPic(@RequestBody PicVo picVo) {
        Result result = ResultUtil.success();
        try {
            result = picService.getPic(picVo);
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

    @PostMapping("/delPic")
    public Result delUser(@RequestBody List<String> PicIds) {
        Result result = ResultUtil.success();
        try {
            result = picService.delPic(PicIds);
            return result;
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

}
